//
//  BaseViewController.m
//  CloudWeibo
//
//  Created by CLOUD on 11/14/14.
//  Copyright (c) 2014 Cloud. All rights reserved.
//

#import "BaseViewController.h"
#import "CLOUDAppDelegate.h"
#import "UIFactory.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.isBackButton = YES;
        self.isCancelButton = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *viewControllers = self.navigationController.viewControllers;
    if ([viewControllers count] > 1 && self.isBackButton) {
        UIButton *button = [UIFactory createButton:@"navigationbar_back.png" highlited:@"navigationbar_back_highlighted.png"];
        button.showsTouchWhenHighlighted = YES;
        button.frame = CGRectMake(0, 0, 24, 24);
        [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.leftBarButtonItem = [leftItem autorelease];
    }
    
    if (self.isCancelButton) {
        UIButton *cancelButton = [UIFactory createNavigationButton:CGRectMake(0, 0, 45, 30) title:@"取消" target:self action:@selector(cancelAction)];
        UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
        self.navigationItem.leftBarButtonItem = [cancelBarButton autorelease];
    }
}

- (void)cancelAction
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)showLoading:(BOOL) show
{
    if (_loadView == nil) {
        _loadView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight/2-80, ScreenWidth, 20)];
        
        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [activityView startAnimating];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.text= @"正在加载中...";
        label.font = [UIFont boldSystemFontOfSize:16.0];
        [label sizeToFit];
        
        label.left = (320-label.width)/2;
        activityView.right = label.left -5;
        
        [_loadView addSubview:label];
        [_loadView addSubview:activityView];
        [label release];
        [activityView release];
        
    }
    
    
    if (show) {
        if (![_loadView superview]) {
            [self.view addSubview:_loadView];
        }
    } else {
        [_loadView removeFromSuperview];
    }
}

- (void)showStatusTip:(BOOL)show title:(NSString *)title
{
    if (_tipWindow == nil) {
        _tipWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
        _tipWindow.windowLevel = UIWindowLevelStatusBar;
        _tipWindow.backgroundColor = [UIColor blackColor];
        
        UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.font = [UIFont systemFontOfSize:13];
        tipLabel.textColor = [UIColor whiteColor];
        tipLabel.backgroundColor = [UIColor clearColor];
        tipLabel.tag = 2015;
        [_tipWindow addSubview:tipLabel];
        
        UIImageView *progress = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20-6, 100, 6)];
        progress.image = [UIImage imageNamed:@"queue_statusbar_progress.png"];
        progress.tag = 2014;
        [_tipWindow addSubview:progress];
    }
    
    UILabel *tipLabel = (UILabel *)[_tipWindow viewWithTag:2015];
    
    UIImageView *progress = (UIImageView *)[_tipWindow viewWithTag:2014];
    
    if (show) {
        tipLabel.text = title;
        _tipWindow.hidden = NO;
        
        progress.left = 0;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:2];
        [UIView setAnimationRepeatCount:100];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        progress.left = ScreenWidth;
        [UIView commitAnimations];
    } else {
        progress.hidden = YES;
        tipLabel.text = title;
        [self performSelector:@selector(removeTipWindow) withObject:nil afterDelay:3];
    }
}

- (void)removeTipWindow
{
    _tipWindow.hidden = YES;
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (SinaWeibo *)sinaweibo
{
    CLOUDAppDelegate *delegate = (CLOUDAppDelegate *)[UIApplication sharedApplication].delegate;
    return delegate.sinaWeibo;
}

- (CLOUDAppDelegate *) appDelegate
{
    CLOUDAppDelegate *appDelegate = (CLOUDAppDelegate *)[UIApplication sharedApplication].delegate;
    return appDelegate;
}

 - (void) setTitle:(NSString *)title
{
    //UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    //titleLabel.textColor = [UIColor blackColor];
    UILabel *titleLabel = [UIFactory createLabel:kNavigationBarTitleLabel];
    titleLabel.font = [UIFont systemFontOfSize:18.0f];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = title;
    [titleLabel sizeToFit];
    self.navigationItem.titleView = titleLabel;
}

@end
